package com.hr.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.hr.common.retrun.R;
import com.hr.system.entity.SysRole;
import com.hr.system.entity.SysRoleMenu;
import com.hr.system.entity.SysUserRole;
import com.hr.system.entity.param.RoleMenuUpdate;
import com.hr.system.mapper.SysRoleMapper;
import com.hr.system.mapper.SysRoleMenuMapper;
import com.hr.system.mapper.SysUserRoleMapper;
import com.hr.system.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hr
 * @since 2024-04-26
 */
@Service
public class SysRoleServiceImpl implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Override
    public List<SysRole> getList(SysRole sysRole) {
        return sysRoleMapper.selectList(Wrappers.<SysRole>lambdaQuery()
                .orderByAsc(true,SysRole::getOrderId));
    }

    @Override
    public Integer add(SysRole sysRole) {
        return sysRoleMapper.insert(sysRole);
    }

    @Override
    public Integer update(SysRole sysRole) {
        return sysRoleMapper.updateById(sysRole);
    }

    @Override
    public Integer delete(Integer id) {
        return sysRoleMapper.deleteById(id);
    }

    @Override
    public Integer changeState(Integer id,Integer state) {
        List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
        if(sysUserRoles.size()>0){
            //有用户不可更改状态
            return -1;
        }else{
            int i = sysRoleMapper.changeState(id,state);
            return i;
        }
    }

    @Transactional
    @Override
    public Integer deleteMult(List<Integer> ids) {
        for(Integer id:ids){
            List<SysUserRole> sysUserRoles = sysUserRoleMapper.selectList(new LambdaQueryWrapper<SysUserRole>().eq(SysUserRole::getRoleId, id));
            if(sysUserRoles.size()>0){
                return -1;
            }
        }
        int i = sysRoleMapper.deleteBatchIds(ids);
        return i;
    }

    @Override
    public Integer updateMenu(RoleMenuUpdate update) {
        //设置角色对应的权限
        //先删除原来的
        sysRoleMenuMapper.delByRid(update.getId());
        //后新增本次的
        update.getMids().addAll(update.getPids());//前端传递 选中的菜单id集合和选中的权限id集合
        sysRoleMenuMapper.insertBatch(update.getId(),update.getMids());

        return 1;
    }

    @Override
    public R queryByRid(Integer id) {
        //查询角色对应的权限信息
        List<SysRoleMenu> menuList=sysRoleMenuMapper.selectList(new LambdaQueryWrapper<SysRoleMenu>().eq(SysRoleMenu::getRoleId,id));
        //集合处理 只要权限的id(包含菜单和权限)
        return R.ok(menuList.stream().map(m-> m.getMenuId()).collect(Collectors.toList()));

    }
}
